package com.label.Web;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.label.Config.Logger.LogAnnotation;
import com.label.Model.Common.ResponseBean;
import com.label.Model.Common.Validation.Save;
import com.label.Model.Do.Tag;
import com.label.Model.Query.CourseInfoQuery;
import com.label.Model.Query.TagQuery;
import com.label.Service.TagService;
import org.apache.http.HttpStatus;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping(value = "/tag")
public class TagController {

    @Resource
    private TagService tagService;

    @RequestMapping("/getAllTag")
    @LogAnnotation(module = "标签模块",operator = "获取所有标签")
    @RequiresAuthentication
    public ResponseBean getAllTag(){
        try {
            return new ResponseBean(HttpStatus.SC_OK, "成功!", tagService.list(new QueryWrapper<Tag>().lambda().eq(Tag::getType,0)));
        }catch (Exception e){
            return new ResponseBean(HttpStatus.SC_INTERNAL_SERVER_ERROR, "未知错误！", null);
        }
    }

    @RequestMapping("/addTag")
    @LogAnnotation(module = "标签模块",operator = "添加标签")
    @RequiresAuthentication
    public ResponseBean addTag(@Validated(value = Save.class) @RequestBody TagQuery tagQuery){
        try {
            return new ResponseBean(HttpStatus.SC_OK, "添加成功!", tagService.save(BeanUtil.copyProperties(tagQuery,Tag.class)));
        }catch (Exception e){
            return new ResponseBean(HttpStatus.SC_INTERNAL_SERVER_ERROR, "未知错误！", null);
        }
    }

    @RequestMapping("/getAllTagByCourse")
    @LogAnnotation(module = "标签模块",operator = "获取课程对应标签")
    @RequiresAuthentication
    public ResponseBean getAllTagByCourse(@RequestBody CourseInfoQuery courseInfoQuery){
        try {
            return new ResponseBean(HttpStatus.SC_OK, "查找成功!", tagService.getAllTagByCourse(courseInfoQuery));
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseBean(HttpStatus.SC_INTERNAL_SERVER_ERROR, "未知错误！", null);
        }
    }

    @RequestMapping("/checkTagName")
    @LogAnnotation(module = "标签模块",operator = "检查标签名")
    @RequiresAuthentication
    public ResponseBean checkTagName(@RequestBody TagQuery tagQuery){
        try {
            return new ResponseBean(HttpStatus.SC_OK, "检查成功!", tagService.checkTagName(tagQuery));
        }catch (Exception e){
            return new ResponseBean(HttpStatus.SC_INTERNAL_SERVER_ERROR, "未知错误！", null);
        }
    }
}
